package com.magicalcoder.pigeontest.client.common;

import com.alibaba.fastjson.JSON;
import com.magicalcoder.pigeon.client.PigeonClient;
import com.magicalcoder.pigeon.common.dto.RmtAddress;
import com.magicalcoder.pigeon.zookeeper.ClientFoundService;
import com.magicalcoder.pigeontest.model.AdminUser;
import com.magicalcoder.pigeontest.model.Person;
import com.magicalcoder.pigeontest.service.AdminUserService;
import com.magicalcoder.pigeontest.service.HelloPersonService;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Created by hzhedongyu on 2017/5/27.
 * 无需zookeeper做节点发现 优点简单快捷 缺点无法自动发现服务
 */
public class ClientApp {

    private static String serviceName;
    private static PigeonClient client;
    public static void main(String[] args) {
        client = new PigeonClient();

        //提前配置好服务端的端口 服务名()
        serviceName = "userService";
//        int port = 8880;
        int port = Integer.valueOf(args[0]);//入参是服务端端口号
        CopyOnWriteArrayList<RmtAddress> currentAliveAllServerList = new CopyOnWriteArrayList<RmtAddress>();
        currentAliveAllServerList.add(new RmtAddress("localhost",port,serviceName));
        ClientFoundService.get().setServiceList(currentAliveAllServerList);
        testTx();
    }

    private static void testManyThreads(){
        AtomicInteger integer = new AtomicInteger(0);
        while (true){
            for(int i=0;i<1;i++){
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            TimeUnit.SECONDS.sleep(1);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        AdminUserService adminUserService = client.newInstance(AdminUserService.class,serviceName);
                        //1简单测试取数据
                        /*
                        List<AdminUser> adminUserList = adminUserService.fetchList();
                        System.out.println(JSON.toJSONString(adminUserList)+adminUserList.size());
                        */

                        /*
                        HelloPersonService helloPersonService = client.newInstance(HelloPersonService.class,serviceName);
                        try {
                            List<Person> person = helloPersonService.GetTestPerson("何栋宇",integer.getAndIncrement());
                            System.out.println(person.get(0).getLastName()+person.get(0).getFirstName());
                        }catch (Exception e){
                            e.printStackTrace();
                        }*/
                    }
                }).start();
            }
            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    private static void testTx(){
        AdminUserService adminUserService = client.newInstance(AdminUserService.class,serviceName);
        //2测试事务
        adminUserService.txTest();
    }
}
